﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using KinectGestureRecognition;
using System.Diagnostics;
using System.Threading;

namespace CIM_Showroom
{
    /// <summary>
    /// Interaktionslogik für QR_Code.xaml
    /// </summary>
    public partial class QR_Code : Window
    {

        #region members
        private DispatcherTimer _dispatcherMouseClickTimer = new DispatcherTimer();
        private Border _focusedBorder;
        KinectSensorModul ksm;
        #endregion
        public QR_Code()
        {
            InitializeComponent();
            this.ksm = KinectSensorModul.Instance;
            ksm.OnSkeletonFrameReadyEvent += new KinectSensorModul.SkeletonFrameReadyEvent(ksm_OnSkeletonFrameReadyEvent);
            ksm.OnColorstreamReadyEvent += new KinectSensorModul.ColorStreamReadyEvent(ksm_OnColorstreamReadyEvent);
            _dispatcherMouseClickTimer.Interval = TimeSpan.FromMilliseconds(750);
            _dispatcherMouseClickTimer.Tick += new EventHandler(_dispatcherMouseClickTimer_Tick);
        }

        ~QR_Code()
        {

            UnregisterEvents();
        }

        void UnregisterEvents()
        {
            ksm.OnSkeletonFrameReadyEvent -= new KinectSensorModul.SkeletonFrameReadyEvent(ksm_OnSkeletonFrameReadyEvent);
            ksm.OnColorstreamReadyEvent -= new KinectSensorModul.ColorStreamReadyEvent(ksm_OnColorstreamReadyEvent);
            _dispatcherMouseClickTimer.Tick -= new EventHandler(_dispatcherMouseClickTimer_Tick);
        }
        #region react 2 gui events
        private void borderIN(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border IN");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = Brushes.Green;
            // Activate Mouse Click Timer
            this.MouseClickStart();
        }

        private void borderOUT(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border Out");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = Brushes.Black;
            this._focusedBorder.Background = Brushes.Black;
            this.MouseClickAbort();
        }

        #endregion

        #region Helper Functions
        void MouseClickAbort()
        {
            this._dispatcherMouseClickTimer.Stop();
        }

        void MouseClickStart()
        {
            this._dispatcherMouseClickTimer.Start();
        }
        #endregion

        #region subscribed events
        void _dispatcherMouseClickTimer_Tick(object sender, EventArgs e)
        {

            // Simulate Mouse Click Action
            this._focusedBorder.Background = Brushes.Green;
            Thread.Sleep(600);
            _dispatcherMouseClickTimer.Stop();
 
            if (this._focusedBorder.Name.Equals("borderAbort"))
            {
                this.Close();
            }


        }
        void ksm_OnColorstreamReadyEvent(object sender, BitmapSource colorFrame)
        {

        }

        void ksm_OnSkeletonFrameReadyEvent(object sender, ImageSource skeletonFrame)
        {
            this.imageSkeleton.Source = skeletonFrame;
        }
        #endregion
    }
}
